package abstractComponent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import concreteComponents.Multiplicador;
import concreteComponents.Sumador;

import ar.edu.unq.tpi.pconc.Channel;

abstract public class Transformador {

	static Map<String, Class<? extends Transformador>> map = new HashMap<String, Class<? extends Transformador>>();

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException {
		Set<Channel<Integer>> channelsS = new HashSet<Channel<Integer>>();
		Set<Channel<Integer>> channelsR = new HashSet<Channel<Integer>>();
		map.put("Sumador", Sumador.class);
		map.put("Multiplicador", Multiplicador.class);
		Transformador instance = map.get(args[0]).newInstance();
		String flag="";
		for (int n = 1;n < args.length;n++) {
			
			if(args[n].equals("-cs")){
				flag = "cs";
				continue;
			} 
			
			if(args[n].equals("-cr")){
					flag = "cr";
					continue;
			}
			
			if(flag.equals("cs")){
				int i = Integer.parseInt(args[n]);
				channelsS.add(new Channel<Integer>(i));
			}
			else{
				int i = Integer.parseInt(args[n]);
				channelsR.add(new Channel<Integer>(i));
			}
		}
		while (true) {
			int[] array=new int[channelsR.size()];
			ArrayList<Integer> al =new ArrayList<Integer>();
			for (Channel<Integer> channel : channelsR){
				al.add(channel.receive());
			}
			for (int i=0;i<al.size();i++){
				array[i]=al.get(i);
				
			}
			int toSend = instance.accion(array);
			for (Channel<Integer> channel : channelsS) {
				channel.send(toSend);
			}
		}
	}

	public abstract int accion(int[] ints);
}

